#include <PhysicsSubSystem\PhysicsSubSystem.h>

const char* PhysicsSubSystem::SUBSYSTEMNAME = "PhysicsSubSystem";
const int32_t PhysicsSubSystem::m_iterations = 10;
const int32_t PhysicsSubSystem::m_elasticIterations = 5;

PhysicsSubSystem::PhysicsSubSystem()
{
	m_logger = new NullLogger();
	m_initialized = false;
}

PhysicsSubSystem::~PhysicsSubSystem()
{
	if (m_initialized) shutdown();
}

bool PhysicsSubSystem::initialize()
{
	m_logger->debug("Initializing Chipmunk...");
	cpInitChipmunk();
	std::string chipmunkVersion = std::string(cpVersionString);
	m_logger->debug("Chipmunk initialized. Library version: " + chipmunkVersion);

	m_logger->debug("Allocating simulation space...");
	m_simulationSpace = cpSpaceNew();
	m_simulationSpace->iterations = m_iterations;
	m_simulationSpace->elasticIterations = m_elasticIterations;

	return m_initialized = true;
}

void PhysicsSubSystem::shutdown()
{
	if (m_initialized)
	{
		m_logger->debug("Deallocating physics shapes, bodies and the simulation space...");
		cpSpaceFreeChildren(m_simulationSpace);
		cpSpaceFree(m_simulationSpace);

		m_initialized = false;
	} else
	{
		m_logger->warn("PhysicsSubSystem already shut down!");
	}

}
